// pages/user/user-balance-log/index.js
const util = require('../../../utils/util.js')
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: app.globalData.CustomBar,
    StatusBar: app.globalData.StatusBar,
    theme: app.globalData.theme, //全局颜色变量
    page: {
      searchCount: false,
      current: 1,
      size: 15,
      ascs: '', //升序字段
      descs: 'create_time'
    },
    parameter: {},
    loadmore: true,
    list: [],
    providerInfo: {
      accountBalanceTotal: 0,
      accountBalance: 0
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.initPage()
        .then(res => {
          this.loadProviderInfo()

          this.setData({
            list: [],
            ['page.current']: 1
          })
          this.loadPage()
        })
  },

  loadPage() {
    app.api.providerWithdrawPage(Object.assign({},
        this.data.page,
        util.filterForm(this.data.parameter)
      ))
      .then(res => {
        let list = res.data.records
        this.setData({
          list: [...this.data.list, ...list]
        })
        if (list.length < this.data.page.size) {
          this.setData({
            loadmore: false
          })
        }
      })
  },
  onReachBottom() {
    if (this.data.loadmore) {
      this.setData({
        ['page.current']: this.data.page.current + 1
      })
      this.loadPage()
    }
  },
  // 获取当前师傅信息
  loadProviderInfo() {
    app.api.providerInfoGet().then(res => {
      let data = res.data || {}
      this.setData({
        providerInfo: data
      })
    })
  },
  BackPage() {
    wx.navigateBack({
      delta: 1
    });
  },
  // 跳转提现申请页面
  toWithdrawPage() {
    if (this.data.providerInfo.cardId) {
      wx.navigateTo({
        url: "/pages/user/user-withdraw/index"
      })
    } else {
      // 跳转维护银行卡信息页面
      wx.navigateTo({
        url: "/pages/user/user-bank-card/index"
      })
    }
  }
})